import { Component } from '@angular/core';
import {IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
// import {DeviceListPage} from "../device-list/device-list";
// import {MessagePage} from "../message/message";
import {DeviceRoomChoosePage} from "../device-room-choose/device-room-choose";
import {PublicProvider} from "../../providers/public/public";



@IonicPage()
@Component({
  selector: 'page-device',
  templateUrl: 'device.html',
})
export class DevicePage {

  // 我们要不惜一切代价赢得战争”，但是“你是那个我们吗？你是那个代价


  rooms = ['全部房间'];
  devices = [];
  device_list:any;
  room_list =[];
  choosed_num = 0;


  constructor(public navCtrl: NavController, public navParams: NavParams,public modalCtrl: ModalController, private pub:PublicProvider ) {
  }

  ionViewDidLoad() {
    this.devices = this.pub.device_list;
    this.device_list = this.devices;
    this.set_room();

  }

  choose_room(n){
    this.choosed_num = n;
    if(n == 0) {
      this.device_list = this.devices;
    } else {
      this.device_list = [];
      for(let d of this.devices){
        if(d.position === this.rooms[n] ){
          this.device_list.push(d);
        }
      }
    }
  }

  set_room(){
    for(let i=0;i<this.device_list.length;i++){
      if(this.rooms.indexOf(this.device_list[i].position) < 0  && this.device_list[i].position != '' ){
        this.rooms.push(this.device_list[i].position);
      }else {
        continue;
      }
    }
    if(this.rooms.length <= 6){
      this.room_list = this.rooms;
    } else {
      this.room_list = this.rooms.slice(0,6);
    }
    // this.choosed_room = this.room_list[0];
  }

  all_room(){
    let profileModal = this.modalCtrl.create(DeviceRoomChoosePage, { room: this.rooms});
    profileModal.onDidDismiss(data => {
      console.log(data);
      this.reset_room(data);
    });
    profileModal.present();
  }

  reset_room(k){
    if(k <= 1){
      this.choose_room(k);
    } else {
      let tep = this.rooms[k];
      this.rooms.splice(k ,1);
      // console.log(this.rooms);
      this.rooms.splice(1 ,0, tep);
      this.room_list = this.rooms.slice(0,6);
      this.choose_room(1);

    }
  }

  take_on_off(n) {
    if(this.device_list[n].state == 0) {
      this.device_list[n].state = 1;
    } else {
      this.device_list[n].state = 0;
    }
  }

}



